package com.jinbo.footmessage.order;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.TextView;

import com.jinbo.footmessage.MyApplication;
import com.jinbo.footmessage.R;
import com.jinbo.footmessage.httpbean.OrderBean;
import com.jinbo.footmessage.httpbean.OrdersBean;
import com.jinbo.footmessage.httpretrofit.BusinessController;
import com.jinbo.footmessage.httpretrofit.Listener;
import com.jinbo.footmessage.utils.LocalPreferencesHelper;

import java.util.List;

public class ConfirmedFragment extends Fragment implements OrderActivity.Callbacks ,AbsListView.OnScrollListener{
    ListView list;
    List<OrderBean> orders;
    ConfirmedItemListViewAdapter adapter;
    private View footerView;
    private View loading;//尾部正在在载的布局
    private View finishLoad;
    private TextView finishText;
    @Override
    public void change(List<OrderBean> orders) {
        // TODO Auto-generated method stub;


        finishLoad.setVisibility(View.GONE);
        list.setOnScrollListener(ConfirmedFragment.this);

        this.orders= orders;
        adapter.setorders(this.orders);
        //adapter.notifyDataSetChanged();

        if (orders.size()< 10) {
            //   showToast("数据已加载完");
            //   listView.removeFooterView(footerView);
            loading.setVisibility(View.GONE);
            finishLoad.setVisibility(View.VISIBLE);
            list.setOnScrollListener(null);
            if(orders.size()==0)
                finishText.setText("暂无确认订单");
            else
                finishText.setText("已经加载全部");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.fragment_confirmed, container, false);
        footerView = inflater.inflate(R.layout.listview_footer, null);
        loading = footerView.findViewById(R.id.loading);//加载中，表尾巴
        finishLoad=footerView.findViewById(R.id.load_finish);
        finishText=(TextView)footerView.findViewById(R.id.load_all_text);
        list = (ListView) view.findViewById(R.id.mainListView);
        adapter = new ConfirmedItemListViewAdapter(getActivity(), orders);
        list.addFooterView(footerView);
        list.setAdapter(adapter);

        return view;
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        String user_id= MyApplication.local.getString(LocalPreferencesHelper.USER_ID);
        // 当不滚动时
        if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
            // 判断是否滚动到底部
            if (view.getLastVisiblePosition() == view.getCount() - 1) {
                BusinessController.getInstance().confirmOrder(user_id, orders.size(), 10, new Listener<OrdersBean>() {
                    @Override
                    public void onStart(Object... params) {
                        //showProgressDialog("请稍后...");
                        //mLoadView.setVisibility(View.VISIBLE);
                        //mPullRefreshLayout.setRefreshing(true);
                       // mLoadView.setVisibility(View.VISIBLE);
                        loading.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onComplete(OrdersBean result, Object... params) {
                        //dismissProgressDialog();
                        // mPullRefreshLayout.setRefreshing(false);
                       // mLoadView.setVisibility(View.GONE);
                       // mCallBacks2.change(result.orders);
                        // OrderActivity.this.
                        //orders = result.orders;
                        // mCallBacks2.change(result.orders);

                        finishLoad.setVisibility(View.GONE);
                        //list.setOnScrollListener(CommentFragment.this);
                        orders.addAll(result.orders);
                        adapter.setorders(orders);
                        //adapter.notifyDataSetChanged();

                        if (result.orders.size() < 10) {
                            //   showToast("数据已加载完");
                            //   listView.removeFooterView(footerView);
                            loading.setVisibility(View.GONE);
                            finishLoad.setVisibility(View.VISIBLE);
                            list.setOnScrollListener(null);

                        }
                    }

                    @Override
                    public void onFail(final String msg, Object... params) {
                       // mLoadView.setVisibility(View.VISIBLE);
                        //dismissProgressDialog();
                        //mLoadView.setVisibility(View.GONE);
                        // mPullRefreshLayout.setRefreshing(false);
                        loading.setVisibility(View.GONE);
                        if (TextUtils.isEmpty(msg)) {
                        } else {
                            ((OrderActivity) getActivity()).show(msg);
                        }
                    }
                });
            }
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

    }
/*	@Override
    public void menu2(PopupWindow menu) {
		// TODO Auto-generated method stub
		//this.menu=menu;
	}*/

}
